package com.zzyl.controller.customer;

import cn.hutool.core.bean.BeanUtil;
import com.aliyun.oss.common.utils.RangeSpec;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.controller.BaseController;
import com.zzyl.dto.AddReservationDto;
import com.zzyl.dto.ReservationDto;
import com.zzyl.dto.TimeDto;
import com.zzyl.service.ReservationService;
import com.zzyl.utils.UserThreadLocal;
import com.zzyl.vo.CountByTimeVo;
import com.zzyl.vo.ReservationVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/customer/reservation")
@Api(tags = "客户预约管理")
public class CustomerReservationController extends BaseController {
    @Autowired
    private ReservationService reservationService;

    @GetMapping("/cancelled-count")
    @ApiOperation("查询取消预约数量")
    public ResponseResult<Long> selectCancelCount() {
        Long userId = UserThreadLocal.getUserId();
        Long count = reservationService.selectCancelCount(userId);
        return ResponseResult.success(count);
    }

    @ApiOperation("查询每个时间段剩余预约次数")
    @GetMapping("/countByTime")
    public ResponseResult<List<CountByTimeVo>> selectCountByTime(Long time) {
        List<CountByTimeVo> vo = reservationService.selectCountByTime(time);
        return ResponseResult.success(vo);
    }

    @ApiOperation("新增预约")
    @PostMapping
    public ResponseResult addReservation(@RequestBody AddReservationDto dto) {
        Long userId = UserThreadLocal.getUserId();
        dto.setCreateBy(userId);
        reservationService.addReservation(dto);
        return ResponseResult.success();
    }

    @ApiOperation("分页查询")
    @GetMapping("/page")
    public ResponseResult<PageResponse<ReservationVo>> selectAll(ReservationDto dto) {
        dto.setId(UserThreadLocal.getUserId());
        PageResponse<ReservationVo> vo = reservationService.selectAll(dto);
        return ResponseResult.success(vo);
    }

    @ApiOperation("取消预约")
    @PutMapping("/{id}/cancel")
    public ResponseResult cancelReservation(@PathVariable("id") Long id) {
        reservationService.cancelReservation(id);
        return ResponseResult.success();
    }
}